﻿using System;
using System.Text;

using Mojo;

namespace McDonalds
{
    [ImportClass(String.Empty, "AppSettings")]
    public class AppSettings
    {
        public AppSettings()
        {
        }

        public static string CouponRequestURI = "http://www.mcdonalds.com.cn/mclub/coupon_print2.aspx";
        public static string CouponDirectoryURI = "http://www.mcdonalds.com.cn/mclub/";
        public static RegularExpression RegExp = new RegularExpression("<\\s*\\/?\\s*img\\s*.*?>", "g");
        public static string locale = "zh_cn";
        public static string lastUpdateTime = "";

        public static void Init()
        {
            //set locale according to settings
            var localeCookie = new Mojo.Model.Cookie("_locale");
            var localetemp = localeCookie.Get<string>();
            if (localetemp != null)
            {
                AppSettings.locale = localetemp;
            }
            else
            {
                AppSettings.locale = "zh_cn";
                localeCookie.Put("zh_cn"); //default to zh_cn
            }
            ChangeLocale(AppSettings.locale);

            try
            {
                GetLastUpdateTime();

                //load saved coupon list
                LoadCoupon();
            }
            catch
            {
                Mojo.Log.Error("Application Init Error");
                AppSettings.SetLastUpdateTime(new DateTime("1975/01/01"));
                AppSettings.SaveCoupon();
            }
        }


        public static void ChangeLocale(string newLocale)
        {
            if (newLocale != null)
            {
                AppSettings.locale = newLocale;
            }

            Locale.Set(AppSettings.locale);
        }

        public static object[] LoadCoupon()
        {
            try
            {
                //load coupon list for local cache
                var couponCookie = new Mojo.Model.Cookie("couponList");
                var couponListTemp = couponCookie.Get<Coupon[]>();
                if (couponListTemp != null)
                {
                    MCModels.couponList = couponListTemp;
                }
               }
            catch (Exception e)
            {
                Mojo.Log.Error("Load Coupon Error");
                MCModels.couponList = new Coupon[0];
            }

            return MCModels.couponList;
        }

        public static void SaveCoupon()
        {

            try
            {
                //save coupon list to local cache
                var couponCookie = new Mojo.Model.Cookie("couponList");
                couponCookie.Put(MCModels.GetCouponList());
            }
            catch
            {
                Mojo.Log.Error("Save Coupon Error");
            }
        }

        public static object GetLastUpdateTime()
        {
            var lastUpdateTimeCookie = new Mojo.Model.Cookie("lastUpdateTime");
            var lastUpdateTimeTmp = lastUpdateTimeCookie.Get<string>();
            if (lastUpdateTimeTmp != null)
            {
                AppSettings.lastUpdateTime = lastUpdateTimeTmp;
            }
            else
            {
                AppSettings.lastUpdateTime = "1975/01/01";
            }

            return AppSettings.lastUpdateTime;
        }

        public static void SetLastUpdateTime(DateTime lastUpdateDate)
        {
            AppSettings.lastUpdateTime = Format.FormatDate(lastUpdateDate, new { date = "yyyy-MM-dd", time = "HH:mm" });
            var lastUpdateTimeCookie = new Mojo.Model.Cookie("lastUpdateTime");
            lastUpdateTimeCookie.Put(AppSettings.lastUpdateTime);
        }
    }
}
